package com.jnshu.service.Impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.jnshu.dao.ModelMapper;
import com.jnshu.dto.admin.ModelDto;
import com.jnshu.pojo.Manage;
import com.jnshu.pojo.Model;
import com.jnshu.service.ModelService;
import com.jnshu.tools.binding.JsonUtil;
import com.jnshu.tools.resultUtils.PageBean;
import com.jnshu.tools.resultUtils.RUtil;
import com.jnshu.tools.resultUtils.ResultBean;
import com.jnshu.tools.resultUtils.ResultEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author Martin
 * @date 2019/8/12 21:23
 */
@Slf4j
@Service
public class ModelServiceImpl implements ModelService {

    @Resource
    ModelMapper modelMapper;

    @Override
    public PageBean selectModelList(int pageNum, int pageSize) {
        Page page = PageHelper.startPage(pageNum,pageSize);
        List<Model> modelList = modelMapper.selectModelList();
        log.info("ModelServiceImpl查询出来的模块列表为："+JsonUtil.toJsonString(modelList));
        if (modelList == null){
            log.info("ModelServiceImpl查询数据为空");
            return RUtil.okDefault(0,null,pageNum,pageSize);
        }
        return RUtil.okDefault(page.getTotal(),modelList,pageNum,pageSize);
    }

    @Override
    public ResultBean deleteByPrimaryKey(Long id) {
        log.info("ModelServiceImpl删除的模块id为："+id);
        int result = modelMapper.deleteByPrimaryKey(id);
        log.info("ModelServiceImpl删除结果为："+result);
        if (result == 1){
            return RUtil.ok();
        }else {
            return RUtil.error(ResultEnum.NOTHINGNESS);
        }
    }

    @Override
    public ResultBean insertSelective(ModelDto modelDto,String loginId) {
        Model model = modelMapper.selectByModelName(modelDto.getModeName());
        if (model != null){
            log.info("ModelServiceImpl查出来的model不为空，已有model："+model);
            return RUtil.error(ResultEnum.SAME_NAME);
        }
        log.info("ModelServiceImpl查出来的model为空，添加新增数据");
        Model newModel = new Model();
        BeanUtils.copyProperties(modelDto,newModel);
        newModel.setCreateAt(System.currentTimeMillis());
        newModel.setUpdateAt(System.currentTimeMillis());
        newModel.setCreateBy(Long.valueOf(loginId));
        newModel.setUpdateAt(Long.valueOf(loginId));
        if (modelMapper.insertSelective(newModel) == 1){
            return RUtil.ok();
        }
        return RUtil.error(ResultEnum.ERROR_ADD);
    }


    @Override
    public ResultBean updateByPrimaryKeySelective(ModelDto modelDto,String loginId) {
        log.info("ModelServiceImpl将要修改的model信息调出来");
        Model model = modelMapper.selectByPrimaryKey(modelDto.getId());
        log.info("ModelServiceImpl根据传入数据id查询出要修改的model："+model);
        if (model !=null){
            if (modelDto.getModeName() != null){
                return RUtil.error(ResultEnum.SAME_NAME);
            }
            BeanUtils.copyProperties(modelDto,model);
            model.setUpdateAt(System.currentTimeMillis());
            model.setUpdateBy(Long.valueOf(loginId));
            if (modelMapper.updateByPrimaryKeySelective(model) == 1){
                return RUtil.ok();
            }
        }
        return RUtil.error(ResultEnum.ERROR_NULL_MANAGE);
    }


}
